<?php

require_rewrite(MODULE_ROOT . "manager/admin/LogManager.class.php");

class systemController extends BaseController {

    private $method;

    public function __construct() {
        parent::__construct();
        $this->method = Handler_http::getMethod();
    }

    /**
     * 修改IP
     */
    public function listip() {
        $userlist = new ManagingModule();
        $this->tpl->assign('userlist', $userlist->sql_user());
        $this->tpl->display("system/sys_list.html");
    }

    public function update_adress_ip() {
        if ($this->method == "POST") {
            $userid = $_POST['userid'];
            $username = $_POST['txtUsername'];
            $ipadress = $_POST['ip_adress'];
            $up = new ManagingModule($userid);
            if ($up->auth_sql_ipadress($ipadress)) {
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '修改用户' . $username . '的IP地址限制成功');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                Handler_tool::alertAndRedirect('修改IP成功,如果修改本用户IP,本系统将直接退出！', HTTPFILEURL . '/system/listip/');
            } else {
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '修改用户' . $username . '的IP地址限制失败');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                Handler_tool::alertAndRedirect('修改失败！', HTTPFILEURL . '/system/listip/');
            }
        } else {
            $userid = $_GET['userid'];
            $detail = new ManagingModule($userid);
            $details = $detail->sql_user();
            $this->tpl->assign('userid', $userid);
            $this->tpl->assign('username', $details[0]['username']);
            $this->tpl->assign('ip_adress', $details[0]['ip_adress']);
        }
        $this->tpl->display("system/upAdress.html");
    }

    /**
     * 用户访问管理
     */
    public function pwdExpirationTime() {
        $this->tpl->assign('siteTime', SITE_TIME); //重鉴别时间
        $this->tpl->assign('loginErrorCount', USER_LOGIN_ERROR); //用户登录失败次数
        $this->tpl->assign('expiredTime', EXPIRED_TIME); //密码过期天数
        $this->tpl->assign('pwd_suoding', PWD_SUODING_TIME); //密码锁定时间
        $this->tpl->display("system/config.html");
    }

    public function pwdExpirationTimeAction() {
        $siteTime = $_POST['siteTime'];
        $loginErrorCount = $_POST['loginErrorCount'];
        $expiredTime = $_POST['expiredTime'];
        $pwd_suoding = $_POST['pwd_suoding'];
        if (empty($siteTime)) {
            Handler_tool::alertAndGoBack('请输入用户登录失效时间！');
        } elseif (empty($loginErrorCount)) {
            Handler_tool::alertAndGoBack('请输入用户登录失败次数！');
        } elseif (empty($expiredTime)) {
            Handler_tool::alertAndGoBack('请输入密码过期天数！');
        } elseif (empty($pwd_suoding)) {
            Handler_tool::alertAndGoBack('请输入密码锁定失效时间！');
        }else {
            $manager = new systemManager();
            $msg = $manager->writeConfigFile($siteTime, $loginErrorCount, $expiredTime,$pwd_suoding);
            switch ($msg) {
                case 0:
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '用户访问信息设置成功,登录时间' . $siteTime . '秒,登陆失败次数' . $loginErrorCount . '次,密码过期天数' . $expiredTime . '天');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                    Handler_tool::alertAndRedirect('配置文件写入成功', HTTPFILEURL . '/system/pwdExpirationTime/');
                    break;
                default :
                    $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '用户访问信息设置,文件写入失败');
                    LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                    Handler_tool::alertAndRedirect('不能打开文件', HTTPFILEURL . '/system/pwdExpirationTime/');
                    break;
            }
        }
    }

    /**
     * 设置密码复杂度
     */
    public function setPwd() {
        $data = new PwdModule();
        if ($this->method == "POST") {
            $pwd['type'] = $_POST['pwdType'];
            $pwd['length'] = $_POST['pwdlength'];
            $id = $_POST['ComplexityId'];
            if (empty($pwd['type'])) {
                Handler_tool::alertAndGoBack('请输入要是用的密码复杂度类型！');
            }
            if (empty($pwd['length']) || $pwd['length'] < 8 || $pwd['length'] > 20) {
                Handler_tool::alertAndGoBack('请输入密码复杂度长度,不可以小于8位，大于20位！');
            }
            $data = $data->setPwdComplexity($pwd, $id);
            if ($data) {
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '修改密码复杂度成功');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                Handler_tool::alertAndRedirect('修改成功！', HTTPFILEURL . '/system/setpwd/');
            }else{
                $log_id=LogManager::writeAdminModifyLog($this->user['id'], $this->user['username'], '修改密码复杂度失败');
                LogManager::writeAuditLog($this->user['id'], $this->user['username'], $log_id, 1, 2);
                Handler_tool::alertAndRedirect('修改失败,请重新修改！', HTTPFILEURL . '/system/setpwd/');
            }
        } else {
            $this->tpl->assign('complexity', $data->getPwdComplexity());
        }
        $this->tpl->display("set_pwd/set_pwd.html");
    }
}
